
function StarObj(){
   this.x = 0;
   this.y = 0;
   this.frameNum = 0;
   this.starImg;
   this.deltaTime = 0
   this.xspd ;  
   this.yspd ;
  /* this.draw1 = function(img){
   	    this.imgobj = imgobj;
   	    //注意这里面的this全部指向imgobj 不指向StarObj
   	   
   	    var x = this.x;
   	    var y = this.y;
   	    imgobj.onload = function(){
   	  	     ctx.drawImage(img,x,y);	
   	     } 
   }*/
  this.init = function(){
     this.x = Math.random()*650 +50;
     this.y = Math.random()*450+50;
     this.frameNum = Math.floor(Math.random()*7);

     this.xspd = Math.random()-0.5;
     this.yspd = Math.random()-0.5;
   
  }
  this.updateFrame = function(){
      this.deltaTime = deltaTime;
      if(deltaTime>16){
        this.x += this.xspd*deltaTime*0.02;  //让星星随时间平缓移动
        this.y += this.xspd*deltaTime*0.02;

        if(this.x >750 || this.y > 550 || this.x <50 || this.y<50) {
            this.init();
            return;  //星星已经重生 后面代码无需执行
        }

        ++this.frameNum;
        this.frameNum = this.frameNum%7;
      }
  }
  this.draw = function(){
     var x = this.x;
     var y = this.y;
     var frameNum = this.frameNum;

     ctx.save();

     ctx.globalAlpha = percent;
     //drawImage(img,sx,sy,swidth,sheight,x,y,width,height)
  	 ctx.drawImage(starImg,frameNum*7,0,7,7,x,y,7,7);

     ctx.restore();

  }

}

var num = 100;
var stars = [];

function drawStars(){
  for(var i = 0;i<num;i++){
     stars[i].updateFrame();
     stars[i].draw();
  }
}